package com.kinon.markett2.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kinon.markett2.R;
import com.kinon.markett2.entity.CaidanEntity;
import com.kinon.markett2.tools.HttpTools;

import java.math.BigDecimal;
import java.util.ArrayList;

/**
 * 菜单
 */

public class CaidanAdapter extends BaseAdapter {
    Context context;
    ArrayList<CaidanEntity> caidanList;
    HttpTools httpTools = null;

    public CaidanAdapter(Context context, ArrayList<CaidanEntity> caidanList) {
        super();
        this.context = context;
        this.caidanList = caidanList;
    }

    @Override
    public int getCount() {
        return caidanList.size();
    }

    @Override
    public Object getItem(int position) {
        return caidanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        final ViewHandle handle;
        if (view == null) {
            handle = new ViewHandle();
            view = LayoutInflater.from(context).inflate(R.layout.item_caidan, null);
            handle.tv_id = (TextView) view.findViewById(R.id.tv_id);
            handle.tv_caipin = (TextView) view.findViewById(R.id.tv_caidan);
            handle.tv_amount = (TextView) view.findViewById(R.id.tv_caipinamount);
            handle.tv_price = (TextView) view.findViewById(R.id.tv_caipinprice);
            handle.tv_jine = (TextView) view.findViewById(R.id.tv_jine);
//			handle.tv_sname=(TextView)view.findViewById(R.id.tv_sname);
            view.setTag(handle);
        } else {
            handle = (ViewHandle) view.getTag();
        }
        handle.tv_id.setText(caidanList.get(position).getId());
        handle.tv_caipin.setText(caidanList.get(position).getName());
        handle.tv_amount.setText("×" + caidanList.get(position).getAmount());
        handle.tv_price.setText("￥" + caidanList.get(position).getPrice());
        double xiaoji = Double.parseDouble(caidanList.get(position).getPrice()) * Double.parseDouble(caidanList.get(position).getAmount());
        BigDecimal bg = new BigDecimal(xiaoji);
        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        handle.tv_jine.setText("￥" + f1);

        return view;
    }

    class ViewHandle {
        /**
         * 序号
         */
        TextView tv_id;
        /**
         * 菜品名
         */
        TextView tv_caipin;
        /**
         * 数量
         */
        TextView tv_amount;
        /**
         * 价格
         */
        TextView tv_price;
        /**
         * 金额
         */
        TextView tv_jine;
//		TextView tv_sname;
    }
}
